#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using FreeOnlineApplication.Class.Repository;
using FreeOnlineApplication.Class.Entity;
using System.Collections;

#endregion

namespace FreeOnlineApplication
{
    public partial class CustomerSearchForm : Form
    {
        CustomerRepository m_rep;
        Customer m_subject = new Customer();
        IMainForm m_mainForm;
        IMasterSearchCustomer m_parent;

        public CustomerSearchForm(IMainForm mainform, IMasterSearchCustomer parent)
        {
            InitializeComponent();
            m_rep = new CustomerRepository(mainform.Connection);
            m_mainForm = mainform;
            m_parent = parent;
            loadRecords();
        }
        private void loadRecords()
        {
            listView1.Items.Clear();
            IList records = m_rep.GetAll(m_mainForm.CurrentUnit.ID);
            foreach (Customer d in records)
            {
                ListViewItem item = listView1.Items.Add(d.CODE);
                item.SubItems.Add(d.ID_NO);
                item.SubItems.Add(d.NAME);
                item.SubItems.Add(d.ADDRESS);
                item.SubItems.Add(d.PHONE);
                item.Tag = d;
            }
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            IList records = m_rep.GetAllSearch(textBox1.Text.Trim(), m_mainForm.CurrentUnit.ID);
            if (records.Count == 0)
            {
                ListViewItem item = listView1.Items.Add("Data tidak di temukan");
            }
            foreach (Customer d in records)
            {
                ListViewItem item = listView1.Items.Add(d.CODE);
                item.SubItems.Add(d.ID_NO);
                item.SubItems.Add(d.NAME);
                item.SubItems.Add(d.ADDRESS);
                item.SubItems.Add(d.PHONE);
                item.Tag = d;
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItem == null) return;
            m_subject = (Customer)listView1.SelectedItem.Tag;
            m_parent.UpdateCustomerBySearch(m_subject);
            this.Close();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            listView1_DoubleClick(null, null);
        }

        private void linkLabelCreateCustomer_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            CustomerCreateForm frm = new CustomerCreateForm(UserControlFactory.CUSTOMER, m_mainForm);
            frm.ShowDialog();
        }       
    }
}